package com.vhall.component.controller.data;

import com.vhall.component.controller.data.facade.DataAdminFacade;
import com.vhall.component.entity.account.dto.IndexDto;
import com.vhall.component.entity.data.vo.*;
import com.vhall.component.entity.interactive.vo.DataMarketBaseReqVo;
import com.vhall.component.entity.interactive.vo.QaExportReqVo;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.data.DataAdminService;
import com.vhall.component.service.data.DataExportService;
import com.vhall.component.service.room.RoomInnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author guoxin.wang
 * @date 2021-06-18 10:36
 */
@VhallComponent
@TokenAuth("sys:stat")
public class DataAdminComponent implements DataAdminFacade {

    @Autowired
    private DataAdminService dataAdminService;

    @Autowired
    private RoomInnerService roomInnerService;

    @Autowired
    private DataExportService dataExportService;

    @Override
    @TokenAuth("sys:stat:live")
    public AdminLiveRspVO live(AdminLiveReqVO reqVO) {
        if (StringUtils.isNotBlank(reqVO.getRoomId())) {
            RoomsEntityDTO room = roomInnerService.getRoomInfo(reqVO.getRoomId());
            if (Objects.isNull(room) && StringUtils.isNumeric(reqVO.getRoomId())) {
                room = roomInnerService.getRoomInfoById(Integer.parseInt(reqVO.getRoomId()));
            }
            if (Objects.isNull(room)) {
                throw new BusinessException(BizErrorCode.EMPTY_ROOM);
            }
            reqVO.setIlId(room.getIlId());
        } else {
            reqVO.setIlId(null);
        }
        return dataAdminService.live(reqVO);
    }

    @Override
    @TokenAuth("sys:stat:index")
    public HashMap<String, IndexDto> index() {
        return dataAdminService.index();
    }

    @Override
    @TokenAuth("sys:stat:list")
    public ExportListRspVO exportList(DataExportAdminParamVO reqVO) {
        if (reqVO.getRoomId() != null) {
            RoomsEntityDTO room = roomInnerService.getRoomInfo(reqVO.getRoomId());
            if (null == room) {
                throw new BusinessException(BizErrorCode.EMPTY_ROOM);
            }
        }
        reqVO.setAccountId("0");
        DataExportParamVO paramVO = new DataExportParamVO();
        BeanUtils.copyProperties(reqVO, paramVO);
        paramVO.setStartTime(null);
        paramVO.setEndTime(null);
        return dataExportService.exportListAction(paramVO);
    }

    @Override
    @TokenAuth("sys:stat:export")
    public String exportPvAction(DataExportAdminParamVO reqVO) {
        if (reqVO.getRoomId() != null) {
            RoomsEntityDTO room = roomInnerService.getRoomInfo(reqVO.getRoomId());
            if (null == room) {
                throw new BusinessException(BizErrorCode.EMPTY_ROOM);
            }
        }
        reqVO.setAccountId("0");
        if (reqVO.getStatus() == null) {
            reqVO.setStatus(1);
        }
        DataExportParamVO paramVO = new DataExportParamVO();
        BeanUtils.copyProperties(reqVO, paramVO);
        return dataExportService.exportPvAction(paramVO);
    }

    @Override
    @TokenAuth("sys:stat:msg")
    public Boolean exportMessage(ExportMessageReqVO reqVO) {
        return dataExportService.exportMessage(reqVO, 0);
    }

    @Override
    @TokenAuth("sys:stat:qa")
    public Boolean exportQa(QaExportReqVo reqVO) {
        return dataExportService.exportQa(reqVO);
    }

    @TokenAuth
    @Override
    public void exportDataMarket(DataMarketBaseReqVo reqVO) throws IOException {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        dataExportService.exportDataMarket(reqVO, request, response);
    }

}
